#ifndef _pwm2_c
#define _pwm2_c

// Secondary PWM based on TMR3

// T3CON
// 0bW0WW WWWW
//   | || |||+-- TMR3ON: 1=Enable, 0=Disable
//   | || ||+--- TMR3CS: 1=external source, 0=Fosc
//   | || |+---- T3SYNC: 1=Don't sync external clock, 0=Sync
//   | || +----- T3CCP1: 1=TMR3 is source for CCP, 0=TMR1 is source
//   | ++------- T3CKPS: 11=1:8, 10=1:4, 01=1:2, 00=1:1 
//   +---------- RD16: 1=16 bit r/w, 0=8 bit

#if FOSC == 4000000
#define T3CON_INIT	0b00001001	// Timer ON, 1:1
#elif FOSC == 16000000
#define T3CON_INIT	0b00001001	// Timer ON, 1:1
#else
#error pwm2.c: There is no value for T3CON at the specified clock speed
#endif 

char pwm2_lowTime, pwm2_highTime;


void pwm2_config( void )
{
   T3CON = T3CON_INIT;
   TMR3IP = 1;    // High priority interrupt
}

void pwm2_set( char high, char low )
{
   pwm2_highTime = high;
   pwm2_lowTime = low;
} 

void pwm2_stop( void )
{
   TMR3IE = 0;
   PWM2_PIN = 0;
}

void pwm2_start( void )
{
   TMR3H = 0xff;
   TMR3L = 0xe0;
   TMR3IE = 1;
}

void isr_pwm2( void )
{
   if( PWM2_PIN )
   {
      TMR3L += pwm2_lowTime;
      PWM2_PIN = 0;
   }
   else
   {
      TMR3L += pwm2_highTime;
      PWM2_PIN = 1;
   }

   TMR3H = 0xff;
   TMR3IF = 0;
}

#endif